package com.example.feitou.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private long lastPressedTime = 0;

    private ListView listView;
    private ArrayList<Fruit> fruits = new ArrayList<>();
    private  MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化水果列表
        innitFruit();
        adapter = new MyAdapter(MainActivity.this,fruits);
        listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }

    private void innitFruit() {
        Fruit f1 = new Fruit(R.drawable.apple_pic,"Apple",5);
        Fruit f2 = new Fruit(R.drawable.banana_pic,"Banana",6);
        Fruit f3 = new Fruit(R.drawable.charry_pic,"Charry",7);
        Fruit f4 = new Fruit(R.drawable.grape_pic,"Grape",8);
        Fruit f5 = new Fruit(R.drawable.mango_pic,"Mango",9);
        Fruit f6 = new Fruit(R.drawable.orange_pic,"Orange",1);
        Fruit f7 = new Fruit(R.drawable.pear_pic,"Pear",5);
        Fruit f8 = new Fruit(R.drawable.pineapple_pic,"Pineapple",5);
        Fruit f9 = new Fruit(R.drawable.watermelon_pic,"Watermelon",6);
        fruits.add(f1);
        fruits.add(f2);
        fruits.add(f3);
        fruits.add(f4);
        fruits.add(f5);
        fruits.add(f6);
        fruits.add(f7);
        fruits.add(f8);
        fruits.add(f9);

    }

    @Override
    public void onBackPressed() {
        long currentPressedTime = System.currentTimeMillis();
        if(currentPressedTime - lastPressedTime > 2000){
            lastPressedTime =currentPressedTime;
            Toast.makeText(MainActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show();
        }else {
            super.onBackPressed();
        }
    }
}
